<template>
  <!-- 工作组设置 -->
  <Card style="height:800px">
    <header>
      <span>工作组名称:</span>
      <Select v-model="groupCode" style="width:200px" clearable>
        <Option v-for="item in groupList" :value="item.value" :key="item.value">{{ item.label }}</Option>
      </Select>
      <span style="margin:0px 0px 0px 20px">节点:</span>
      <Select v-model="nodeCode" style="width:200px" clearable>
        <Option v-for="item in nodeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
      </Select>
      <span style="margin:0px 0px 0px 20px">用户:</span>
       <Select v-model="userCode" style="width:200px" filterable clearable>
        <Option v-for="item in userList" :value="item.value" :key="item.value">{{ item.label }}</Option>
      </Select>
      <Button type="primary" @click="initPage" style="margin:0px 0px 0px 20px;width: 110px">查询</Button>
      <Button type="primary" @click="addWorkGroup" style="margin:0px 0px 0px 20px;width: 110px">新增工作组</Button>
    </header>
    <Content style="margin-top:20px;">
      <Table  border :columns="tableColumns" :data="tableDatas"></Table>
    </Content>
    <div style="text-align:center;width:50%;margin:100px auto;">
      <Page :total="totalPage" :current="pageNum" :page-size="pageSize" size="small" show-elevator show-total @on-change="handlePageChange"></Page>
    </div>
  </Card>
</template>

<script>

export default {

  data () {
    return {
      groupCode: null,
      nodeCode: null,
      userCode: null,
      groupList: [],
      nodeList: [],
      userList: [],
      tableColumns: [
        {
          type: 'index',
          title: '序号'
        },
        {
          title: '工作组名称',
          key: 'groupName',
          render: (h, param) => {
            var vm = this
            return h('div', [
              h('a', {
                style: {
                  textDecoration: 'underline'
                },
                on: {
                  'click' () {
                    vm.toEditPage('addworkgroup', param.row, false)
                  }
                }
              }, param.row.groupName)
            ])
          }
        },
        {
          title: '优先级',
          key: 'priority'
        },
        {
          title: '是否有效',
          key: 'isValid'
        },
        {
          title: '最近更新人',
          key: 'updatedBy'
        },
        {
          title: '最近更新时间',
          key: 'updatedDate'
        },
        {
          title: '操作',
          render: (h, param) => {
            const vm = this
            if (param.row.deleteFlag === '0') {
              return h('div', {}, [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    fontSize: '12px'
                  },
                  on: {
                    click: () => {
                      vm.toEditPage('addworkgroup', param.row, true)
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    fontSize: '12px'
                  },
                  on: {
                    click: () => {
                      vm.deleteGroup(param.row)
                    }
                  }
                }, '删除')
              ])
            }
          }
        }
      ],
      tableDatas: [],
      totalPage: 0,
      pageNum: 1,
      pageSize: 12
    }
  },
  methods: {
    handlePageChange: function (pageNum) {
      var vm = this
      vm.pageNum = pageNum
      vm.initPage()
    },
    toEditPage (toPage, row, isEditable) {
      if (row.id != null) {
        this.$router.replace({ name: toPage, params: { id: row.id, deleteFlag: row.deleteFlag, isEditable: isEditable } })
      } else {
        this.$Message.warning('参数为空')
      }
    },
    addWorkGroup () {
      this.$router.replace({ name: 'addworkgroup', params: {deleteFlag: '0', isEditable: true} })// 类似post传参
    },
    initSelectDatas () {
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/workgroup/service/selectDropDownDatas').then(re => {
        var data = re.data.data || []
        // 初始化工作组下拉框
        this.groupList = []
        var fData = {}
        fData.label = '请选择'
        fData.value = ''
        this.groupList.push(fData)
        data.groupList.forEach(item => {
          var sData = {}
          sData.value = item.groupCode
          sData.label = item.groupName
          this.groupList.push(sData)
        })
        // 初始化节点下拉框
        this.nodeList = []
        var nData = {}
        nData.label = '请选择'
        nData.value = ''
        this.nodeList.push(nData)
        data.nodeList.forEach(item => {
          var sData = {}
          sData.value = item.nodeCode
          sData.label = item.nodeName
          this.nodeList.push(sData)
        })
        // 初始化用户
        this.userList = []
        var uData = {}
        uData.label = '请选择'
        uData.value = ''
        this.userList.push(nData)
        data.userList.forEach(item => {
          var sData = {}
          sData.value = item.userCode
          sData.label = item.userNamep
          this.userList.push(sData)
        })
      }).catch(er => {
      })
    },
    // 初始化列表页
    initPage () {
      var vm = this
      var params = {}
      params.groupCode = vm.groupCode
      params.pageSize = vm.pageSize
      params.curPage = vm.pageNum
      params.nodeCode = vm.nodeCode
      params.userCode = vm.userCode
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/workgroup/service/selectGroupWorkInfoList', params).then(re => {
        this.totalPage = re.data.data.total
        this.curPage = re.data.curPage
        this.tableDatas = []
        var datas = re.data.data.list || []
        datas.forEach(item => {
          var tData = {}
          tData.groupCode = item.groupCode
          tData.groupName = item.groupName
          if (item.deleteFlag === 0) {
            tData.isValid = '有效' // '0有效  1失效'
            tData.deleteFlag = '0'
          } else {
            tData.isValid = '无效'
            tData.deleteFlag = '1'
          }
          tData.updatedBy = item.updatedBy
          tData.updatedDate = this.$util.formatTime(new Date(item.updatedDate))
          tData.id = item.id
          tData.priority = item.priority
          this.tableDatas.push(tData)
        })
      }).catch(er => {
      })
    },
    // 置为失效
    deleteGroup (row) {
      var param = {
        id: row.id,
        deleteFlag: '1',
        groupCode: row.groupCode
      }
      this.$ajax.post(this.$appContext.marvels_core_approve + 'approve/workgroup/service/updateGroupDatas', param).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
        } else {
          this.initPage()
          this.$Message.success('更新成功')
        }
      }).catch(er => {
      })
    }
  },
  created: function () {
    this.$ajax = this.$util.ajax()
    this.initSelectDatas()
    this.initPage()
  }
}
</script>

<style lang="less">

</style>
